//
//  songView.m
//  SimpleiPodLibrary
//
//  Created by 畑圭輔 on 09/11/21.
//  Copyright 2009 Keisuke Hata. All rights reserved.
//

#import <MediaPlayer/MediaPlayer.h>
#import "UIiPodSongView.h"


@implementation UIiPodSongView

/*
- (id)initWithStyle:(UITableViewStyle)style {
    // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
    if (self = [super initWithStyle:style]) {
    }
    return self;
}
*/

- (void)viewDidLoad {
    [super viewDidLoad];

	// 全楽曲のクエリーからコレクションを取得
	MPMediaQuery *songsQuery = [MPMediaQuery songsQuery];
	NSArray *items = [songsQuery collections];
	
	// 曲数を取得する
	int songNum = [items count];
	NSLog(@"songs num : %d" , songNum);

	// 楽曲情報を格納する配列を生成する
	_songs = [[NSMutableArray alloc] initWithCapacity:songNum];
	// 曲名を格納する
	for (MPMediaItemCollection *song in items) 
	{
		MPMediaItem *song_representativeItem = 
			song.representativeItem;
		if( song_representativeItem == nil )
			song_representativeItem = [song.items objectAtIndex:0];
			
		NSString *title = [song_representativeItem
						   valueForProperty:MPMediaItemPropertyTitle];
		
		if(title == nil )
			title = @"不明な曲";
		[_songs addObject:title];
	}
}

/*
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
*/
/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
	[super viewDidDisappear:animated];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// テーブルビューセルの数を返す
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	
	// 曲数
    return [_songs count];
}

// テーブルビューセルを表示する
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
	int index = [indexPath row];
	const int labelTag = 0x2;
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
									   reuseIdentifier:CellIdentifier] autorelease];
		UILabel *label = [[[UILabel alloc] 
						   initWithFrame:CGRectMake( 0, 0, 
													cell.contentView.bounds.size.width, 
													cell.contentView.bounds.size.height)] autorelease];
		[label setTag:labelTag];
		[cell.contentView addSubview:label];
    }

	// ラベルのタグからUILabelを取り出して、曲名をセットします。
	UILabel *label = (UILabel *)[cell viewWithTag:labelTag];
	label.text = [_songs objectAtIndex:index];
	
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
	// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
	// [self.navigationController pushViewController:anotherViewController];
	// [anotherViewController release];
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


- (void)dealloc {
	[_songs release];
    [super dealloc];
}


@end

